package cn.cdut.yiwenqian.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.cdut.yiwenqian.dao.interfaces.ITitleDao;
import cn.cdut.yiwenqian.entity.CdutPostTitles;
import cn.cdut.yiwenqian.entity.CdutPostTitletypes;
import cn.cdut.yiwenqian.util.MyPageToolVo;
@Scope("singleton")
@Repository
public class TitleDaoImpl extends HibernateDaoSupport implements ITitleDao {
	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}

	@Override
	public CdutPostTitles findTitleById() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MyPageToolVo<CdutPostTitles> findAllTitleByUserId(final Integer uid, final int page) {
		final MyPageToolVo<CdutPostTitles> resultPage=new MyPageToolVo<CdutPostTitles>();
		List<CdutPostTitles> list=(List<CdutPostTitles>) this.getHibernateTemplate().execute(new HibernateCallback<List<CdutPostTitles>>(){

			@Override
			public List<CdutPostTitles> doInHibernate(Session session) throws HibernateException {
				StringBuffer str=new StringBuffer();
				str.append("from CdutPostTitles t where t.cdutPostUsers.unumber=?");
				Query query=session.createQuery(str.toString());
				query.setParameter(0, uid);
				resultPage.setPageCount(query.list().size());
				resultPage.setPageNum(page);
				query.setFirstResult(15*(page-1));
				query.setMaxResults(15);
				return query.list();
		}});
		resultPage.setList(list);
		return resultPage;
	}

	@Override
	public List<CdutPostTitles> findAllTitleByType(CdutPostTitletypes type, int page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CdutPostTitles> findAllTitle(int page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CdutPostTitles> findTitlesByName(String name, int page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveTitle(CdutPostTitles title) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateTitle(CdutPostTitles title) {
		// TODO Auto-generated method stub
		
	}
}
